const {getMenu,getHistory,getAddOrdering,deleteCancelOrdering,getUserOrdering,getOrdering,updateCloseOrdering,getUser} = require("../model/ordering");

//今日菜品
module.exports.handleMenu=async (ctx,next)=>{
    let sqMenu=await getMenu()
    // console.log(sqAbout)
    if (sqMenu[0]){
        ctx.body = {
            status: 200,
            data:sqMenu,
            message: '获取成功'
        }
    }else {
        ctx.body = {
            status: 200,
            message: '今日菜品暂未发布'
        }
    }
}

//所有历史菜品
module.exports.handleHistory=async (ctx,next)=>{
    let sqHistory=await getHistory()
    // console.log(sqAbout)
    if (sqHistory[0]){
        ctx.body = {
            status: 200,
            data:sqHistory,
            message: '获取成功'
        }
    }
}

//添加订单
module.exports.handleAddOrdering=async (ctx,next)=>{
    let { user_id,menu_id,menu_type} = ctx.query
    // console.log(user_id,menu_id)
    let sqAddOrdering=await getAddOrdering(user_id,menu_id,menu_type)
    // console.log(sqAddOrdering)
    if (sqAddOrdering.fieldCount==0){
        ctx.body = {
            status: 200,
            message: '订单添加成功'
        }
    }
}

//取消订单
module.exports.handleCancelOrdering=async (ctx,next)=>{
    let { user_id,menu_id,cancelValue} = ctx.query
    console.log(ctx.query)
    let sqCancelOrdering=await deleteCancelOrdering(user_id,menu_id,cancelValue)
    // console.log(sqAddOrdering)
    if (sqCancelOrdering.fieldCount==0){
        ctx.body = {
            status: 200,
            message: '取消订单成功'
        }
    }
}

//用户订单菜品列表
module.exports.handleUserOrdering=async (ctx,next)=>{
    let { user_id } = ctx.query
    // console.log(user_id,menu_id)
    // let y=[]
    //获取用户订单列表
    let sqOrdering=await getOrdering(user_id)
    // console.log(sqOrdering)
    //获取用户菜品列表
    let sqUserOrdering=await getUserOrdering(user_id)
    //给菜品信息追加菜品数量和订单状态
    sqOrdering?.map((item,index)=>{
            if (item.menu_id==sqUserOrdering[index].menu_id){
              return (sqUserOrdering[index].order_count=item.order_count ,
                sqUserOrdering[index].      order_status=item.order_status),
                  sqUserOrdering[index].    order_id=item.order_id
            }
        // sqUserOrdering?.map((jtem)=>{
        //     if (item.menu_id==jtem.menu_id){
        //        return  y.push({
        //             ...jtem,
        //             order_count:item.order_count ,
        //             order_status:item.order_status,
        //             order_id:item.order_id
        //         })
        //     }
        // })
    })
    // console.log(sqAddOrdering)
    if (sqUserOrdering[0]){
        ctx.body = {
            status: 200,
            data:sqUserOrdering,
            message: '用户订单菜品获取成功'
        }
    }else {
        ctx.body = {
            status: 200,
            message: '用户未有订单菜品'
        }
    }
    return {sqUserOrdering}
}

//结算
module.exports.handleCloseOrdering=async (ctx,next)=>{
    let { user_id,total,type} = ctx.query
    total=Number(total)
    // console.log(user_id,total=Number(total),type)
    //今日菜单列表
    // let {sqUserOrdering}=await this.handleUserOrdering(ctx)
    // console.log(sqUserOrdering)
    // let time=new Date().getHours()
    // //总价
    //
    // let total=0
    //     sqUserOrdering.forEach((item)=>{
    //         if (item.order_status=='false'){
    //             total-=(Number(item.menu_price)*item.order_count)
    //         }
    // })
    //检查用户金额
    let sqUser= await getUser(user_id)
    // console.log(sqUser[0].user_money+total)
    if (sqUser[0].user_money+total<0) {
       return  ctx.body = {
            status: 404,
            message: `余额不足！`
        }
    }
    //修改用户今日订单的状态 及 用户的零钱
    let sqCloseOrdering=await updateCloseOrdering(user_id,total,type)
    // console.log(sqCloseOrdering)
    if (sqCloseOrdering.fieldCount==0){
        ctx.body = {
            status: 200,
            message: `结算成功共支付${total}块`
        }
    }else {
        ctx.body = {
            status: 404,
            message:'结算失败'
        }
    }
}
